﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL ;

public partial class Controls_Products_ctrProductCategoryMenu : System.Web.UI.UserControl
{
    private int nCurrentLanguage = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage(); 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitData();
            this.BindDataProductCategory();
          
        }

    }
    private void InitData()
    {
        lnkProduct.Text = MultipleLanguage.PRODUCT[nCurrentLanguage - 1].ToUpper();
        lnkProduct.NavigateUrl  = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY; 
    }

    #region Grid Processing

    private void BindDataProductCategory()
    {
        ProductCategory objProductCategory = new ProductCategory();
        DataTable dtbProductCategory = new DataTable ();
        dtbProductCategory = objProductCategory.SearchByParent(0);
        rptProduct0.DataSource = dtbProductCategory;
        rptProduct0.DataBind();
    }

    public string GetCurrentPageName()
    {
        string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
        System.IO.FileInfo oInfo = new System.IO.FileInfo(sPath);
        string sRet = oInfo.Name;
        return sRet;
    }

    protected void rptProduct0_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            HyperLink lnkProduct0 = (HyperLink)e.Item.FindControl("lnkProduct0");
            Repeater rptProduct1 = (Repeater)e.Item.FindControl("rptProduct1");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductCategoryID"));
            int nParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));
            switch (nCurrentLanguage)
            {
                case 1:
                    lnkProduct0.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName1"));
                    break;
                case 2:
                    lnkProduct0.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName2"));
                    break;
                case 3:
                    lnkProduct0.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName3"));
                    break;
            }

            lnkProduct0.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY   + "?" + Config.PRO_CAT + "=" + nID;
            
            ProductCategory objProduct0Category = new ProductCategory();
            DataTable dtbProduct0Category = new DataTable();
            dtbProduct0Category=  objProduct0Category.SearchByParent(nID);
            int nCOuntItem = dtbProduct0Category.Rows.Count;
            rptProduct1.DataSource = dtbProduct0Category;
            rptProduct1.DataBind();
        }
    }
    protected void rptProduct1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            HyperLink lnkProductParent = (HyperLink)e.Item.FindControl("lnkProductParent");
            Repeater rptProductChild1 = (Repeater)e.Item.FindControl("rptProductChild1");

            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductCategoryID"));
            int nParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));
            switch (nCurrentLanguage)
            { 
                case 1:
                    lnkProductParent.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName1"));
                    break;
                case 2:
                    lnkProductParent.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName2"));
                    break;
                case 3:
                    lnkProductParent.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName3"));
                    break;
            }

            lnkProductParent.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + nID;

           ProductCategory objChild = new ProductCategory();
           DataTable dtbChild = new DataTable();
           dtbChild = objChild.SearchByParent(nID);
           rptProductChild1.DataSource = dtbChild;
           rptProductChild1.DataBind();
        }
    }
    protected void rptProductChild1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            HyperLink lnkProductChild = (HyperLink)e.Item.FindControl("lnkProductChild");
            HyperLink lnkCountChild = (HyperLink)e.Item.FindControl("lnkCountChild");
            Repeater rptProductChild12 = (Repeater)e.Item.FindControl("rptProductChild12");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductCategoryID"));
            int nParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));
          
            switch (nCurrentLanguage)
            {
                case 1:
                    lnkProductChild.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName1"));
                    break;
                case 2:
                    lnkProductChild.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName2"));
                    break;
                case 3:
                    lnkProductChild.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName3"));
                    break;
            }
            
            int nCategoryID = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_CAT]);
            if (nCategoryID == nID)
            { 
                lnkProductChild.ForeColor = System.Drawing.Color.Red;
            }

            ProductCategory objProductCategory = new ProductCategory();
            DataTable dtbProductCategory = new DataTable();
            dtbProductCategory = objProductCategory.SearchByParent(nID);
            int nItemCount = dtbProductCategory.Rows.Count;

            if (nItemCount > 0)
            {
               
                rptProductChild12.DataSource = dtbProductCategory;
                rptProductChild12.DataBind();
               
            }
            else
            {
                lnkProductChild.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + nID;
            }
        }
    }
    protected void rptProductChild12_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            HyperLink lnkProductChild2 = (HyperLink)e.Item.FindControl("lnkProductChild2");
          
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "ProductCategoryID"));
            int nParent = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Parent"));

            switch (nCurrentLanguage)
            {
                case 1:
                    lnkProductChild2.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName1"));
                    break;
                case 2:
                    lnkProductChild2.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName2"));
                    break;
                case 3:
                    lnkProductChild2.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "ProductCategoryName3"));
                    break;
            }
            lnkProductChild2.NavigateUrl = Config.ROOT + Pages.FrontEnds.PRODUCT_CATEGORY + "?" + Config.PRO_CAT + "=" + nID;
        }
    }
    #endregion
}